﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 实验7_学生成绩管理系统
{
    public partial class FormBase : Form
    {
        string myConnString = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master";
        string sqlSelectAll = null;
        public FormBase()
        {
            InitializeComponent();
        }
        public FormBase(string sql)
        {
            InitializeComponent();
            sqlSelectAll = sql;
            updateDataGridView(this.dataGridView1, this.bindingNavigator1, sql);
        }

        private void FormBase_Load(object sender, EventArgs e)
        {

        }

        public void updateDataGridView(DataGridView dgv, BindingNavigator bn, string sql)
        {
            DBHelper dbhelper = new DBHelper();
            DataTable dt = dbhelper.GetDataTable(sqlSelectAll, null, null);

            BindingSource bs = new BindingSource();
            DataView dv = dt.DefaultView;
            bs.DataSource = dv;
            dgv.DataSource = bs;
            bn.BindingSource = bs;
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            // 输出操作记录
            AddRecord("添加" + ((DataRowView)this.bindingNavigator1.BindingSource.Current).Row.ItemArray[0] + "，点击保存后生效！");
        }

        private void tsb_copy_Click(object sender, EventArgs e)
        {

        }

        public BindingSource GetBsByGrid(DataGridView dgv)
        {
            BindingSource bs = null;
            if (dgv.DataSource is BindingSource)
            {
                bs = dgv.DataSource as BindingSource;
            }
            return bs;
        }

        private void tsb_refresh_Click(object sender, EventArgs e)
        {
            updateDataGridView(this.dataGridView1, this.bindingNavigator1, sqlSelectAll);
        }

        // 虚方法需要子类重写
        protected virtual void tsb_save_Click(object sender, EventArgs e)
        {
            MessageBox.Show("保存按钮的虚方法，请重写");
        }
        public DataTable GetDataTableByGrid(DataGridView dgv)
        {
            DataTable dt = null;
            if (dgv.DataSource is BindingSource)
            {
                BindingSource bs = dgv.DataSource as BindingSource;
                if (bs.DataSource is DataView)
                {
                    DataView dv = bs.DataSource as DataView;
                    dt = dv.Table;
                }
            }
            return dt;
        }

        public DataTable GetDateTable()
        {
            DataTable dtSource = GetDataTableByGrid(dataGridView1);
            return dtSource;
        }

        protected virtual void btn_selectByKey_Click(object sender, EventArgs e)
        {
            MessageBox.Show("模糊查询虚方法，请重写");
        }
        public void RefreshView(DataTable dt)
        {
            BindingSource bs = new BindingSource();
            DataView dv = dt.DefaultView;
            bs.DataSource = dv;
            this.dataGridView1.DataSource = bs;
            this.bindingNavigator1.BindingSource = bs;
        }

        public string GetSelectKey()
        {
            return this.tb_selectKey.Text.ToString();
        }
        public void AddRecord(String msg)
        {
            this.listBox1.Items.Add(msg);
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            // 输出操作记录
            AddRecord("删除" + ((DataRowView)this.bindingNavigator1.BindingSource.Current).Row.ItemArray[0] + "，点击保存后生效！");
        }

        private void clikRecord_Click(object sender, EventArgs e)
        {
            string text = (sender as ToolStripButton).Text.ToString();
            // 输出操作记录
            AddRecord(text + ((DataRowView)this.bindingNavigator1.BindingSource.Current).Row.ItemArray[0] + "，点击保存后生效！");
        }

        

    }
}
